import 'package:flutter/material.dart';

import '../../config/public_color.dart';

/// FileName require_text
///
/// @Author lax
/// @Date 2023/4/3
///
/// @Description

enum RequireTextPosition {
  left,
  right,
}

class RequireText extends StatelessWidget {
  static RequireTextPosition defaultPosition = RequireTextPosition.left;

  final bool required;
  final String? text;
  late final TextStyle style;
  late final RequireTextPosition position;

  final Widget? textWidget;
  final int? flex;

  RequireText({
    super.key,
    this.required = true,
    this.text,
    TextStyle? style,
    RequireTextPosition? position,
    this.textWidget,
    this.flex = 1,
  }) {
    this.style = style ?? TextStyle(fontSize: 14, color: PublicColor.darkText);
    this.position = position ?? defaultPosition;
  }

  @override
  Widget build(BuildContext context) {
    final widget = textWidget ?? Text(text ?? '', style: style);
    if (!required) {
      return widget;
    }
    return Row(
      children: [
        if (position == RequireTextPosition.left)
          Text(
            required ? ' *' : '',
            style: TextStyle(fontSize: style.fontSize, color: Colors.red),
          ),
        flex != null
            ? Flexible(
                flex: flex!,
                child: widget,
              )
            : widget,
        if (position == RequireTextPosition.right)
          Text(
            required ? ' *' : '',
            style: const TextStyle(fontSize: 14, color: Colors.red),
          ),
      ],
    );
  }
}
